package com.coba.killthispage;


import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author USER
 */
public class ScreenThree
        extends GameCanvas
        implements Runnable {

    private AnimatedSprite spSpiral;
    private static final int FRAME_WIDTH = 57;
    private static final int FRAME_HEIGHT = 53;
    private int canvas_width, canvas_height;
    private StarSprite spStar;
    private LayerManager lmgr;
    private boolean running = false;
    private KillThisPageMIDlet midlet;

    public ScreenThree(KillThisPageMIDlet midlet) {
        super(false);

        this.midlet = midlet;

        try {
            // nonanimated sprites
            spStar = new StarSprite(Image.createImage("/star.png"), 150, 200);
                        
            //animated sprite
            spSpiral = new AnimatedSprite(Image.createImage("/spiral.png"),
                    FRAME_WIDTH, FRAME_HEIGHT);

            //change the reference pixel to the middle of sprite
            spSpiral.defineReferencePixel(FRAME_WIDTH / 2, FRAME_HEIGHT / 2);

            //center the sprite on the canvas
            //(center of sprite is now in center of display)
            spSpiral.setRefPixelPosition(getWidth() / 2, getHeight() / 2);

            // create and add to layer manager
            lmgr = new LayerManager();
            lmgr.append(spSpiral);
            lmgr.append(spStar);
        } catch (Exception e) {
        }

        // save canvas width and height
        canvas_width = getWidth();
        canvas_height = getHeight();
    }

    //start thread
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }
    //game loop

    public void run() {
        Graphics g = getGraphics();

        while (running) {
            // look for keypress
            checkForKeys();

            if (checkForCollision() == false) {
                drawDisplay(g);
            } else {
                //flash backlight and vibrate device
                midlet.display.flashBacklight(500);
                midlet.display.vibrate(500);
            }

            try {
                Thread.sleep(50);
            } catch (InterruptedException ie) {
            }
        }
    }

    private void checkForKeys() {
        int keyState = getKeyStates();

        if ((keyState & LEFT_PRESSED) != 0) {
            spStar.moveLeft();
        } else if ((keyState & RIGHT_PRESSED) != 0) {
            spStar.moveRight(canvas_width);
        } else if ((keyState & UP_PRESSED) != 0) {
            spStar.moveUp();
        } else if ((keyState & DOWN_PRESSED) != 0) {
            spStar.moveDown(canvas_height);
        }
    }

    protected void keyPressed(int keyCode) {
        if (keyCode == -5) {
        } else if (keyCode == -2) { //down
        } else if (keyCode == -4) { //right
            System.out.println("three : ");
            midlet.beforeShow(3);
        } else if (keyCode == -1) { //up
        } else if (keyCode == -3) { //left
        } else if (keyCode == -7) {
            midlet.exit();
        } else if (keyCode == -6) {
        }
    }

    private boolean checkForCollision() {
        if (spStar.collidesWith(spSpiral, true)) {
            //upon collision, restore the last x/y
            spStar.restoreXY();
            return true;
        } else {
            return false;
        }
    }

    private void drawDisplay(Graphics g) {
        //clear background
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x0000ff);
        g.drawString("Screen Three", 100, 20, Graphics.TOP | Graphics.LEFT);
        
        //animated sprite, show next frame in sequence
        spSpiral.nextFrame();

        //paint all layers
        lmgr.paint(g, 0, 0);

        //flush off-screen buffer to display
        flushGraphics();
    }

    public void stop() {
        running = false;
    }
}
